// pages/releasesign/releasesign.js
var util = require('../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    task: {
      name: 'morning',
      address: '点击选择地点',
      signTime: '00:00',
      signEarlyTime: '00:00',
      startDay: '2019-07-17',
      endDay: '2019-07-17',
      repeat: {
        'monday': 1,
        'tuesday': 1,
        'wednesday': 1,
        'thursday': 1,
        'friday': 1,
        'saturday': 0,
        'sunday': 0
      }
    },
    openId: '',
    userInfo: {},
    creating: false,
    button: {
      txt: '新建'
    },
    modalHidden: true,
    selectData: ['早自习','晚自习','寝室点名','活动讲座'],
    index: 0,
    selectShow: false,//控制下拉列表的显示隐藏，false隐藏、true显示
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 点击下拉显示框
  selectTap() {
    this.setData({
      selectShow: !this.data.selectShow
    });
  },
  optionTap(e) {
    var _this = this
    let Index = e.currentTarget.dataset.index;//获取点击的下拉列表的下标
    _this.setData({
      index: Index,
      selectShow: !this.data.selectShow
    });
    if (Index == 0) {
      _this.setData({
        'task.name': 'morning'
      });
    }
    if (Index == 1) {
      _this.setData({
        'task.name': 'evening'
      });
    }
    if (Index == 2) {
      _this.setData({
        'task.name': 'dormitory'
      });
    }
    if (Index == 3) {
      _this.setData({
        'task.name': 'activity'
      });
    }

  },

  // 设置任务名称
  // bindKeyInput: function (e) {
  //   this.setData({
  //     'task.name': e.detail.value
  //   });
  // },

  // 设置任务地点
  getAddress: function () {
    var that = this
    app.getPermission(that);    //传入that值可以在app.js页面直接设置内容   
  },

  // 设置打卡时间
  setSignTime: function (e) {
    var that = this;
    var hour = ((+e.detail.value.slice(0, 2) + 24 - 1) % 24).toString();
    console.log(hour)
    that.setData({
      'task.signTime': e.detail.value,
      'task.signEarlyTime': (hour[1] ? hour : '0' + hour) + ':' + e.detail.value.slice(3, 5)
    });
  },

  // 设置开始日期
  startDateChange: function (e) {
    this.setData({
      'task.startDay': e.detail.value
    })
  },

  // 设置结束日期
  endDateChange: function (e) {
    this.setData({
      'task.endDay': e.detail.value
    })
  },

  // 设置重复日
  changeMonday: function (e) {
    var state = this.data.task.repeat.monday;
    this.setData({
      'task.repeat.monday': (state == 1 ? 0 : 1)
    });
  },
  changeTuesday: function (e) {
    var state = this.data.task.repeat.tuesday;
    this.setData({
      'task.repeat.tuesday': (state == 1 ? 0 : 1)
    });
  },
  changeWednesday: function (e) {
    var state = this.data.task.repeat.wednesday;
    this.setData({
      'task.repeat.wednesday': (state == 1 ? 0 : 1)
    });
  },
  changeThursday: function (e) {
    var state = this.data.task.repeat.thursday;
    this.setData({
      'task.repeat.thursday': (state == 1 ? 0 : 1)
    });
  },
  changeFriday: function (e) {
    var state = this.data.task.repeat.friday;
    this.setData({
      'task.repeat.friday': (state == 1 ? 0 : 1)
    });
  },
  changeSaturday: function (e) {
    var state = this.data.task.repeat.saturday;
    this.setData({
      'task.repeat.saturday': (state == 1 ? 0 : 1)
    });
  },
  changeSunday: function (e) {
    var state = this.data.task.repeat.sunday;
    this.setData({
      'task.repeat.sunday': (state == 1 ? 0 : 1)
    });
  },

  // 隐藏提示弹层
  modalChange: function (e) {
    this.setData({
      modalHidden: true
    })
  },

  // 创建任务
  createTask: function () {
    var that = this;
    var task = this.data.task;
    var openId = this.data.openId;
    var userInfo = this.data.userInfo;
    var UserInfo = app.globalData.UserInfo
    // console.log(UserInfo)
    var start = task.startDay + ' ' + task.signEarlyTime
    var startTime = (new Date(start)).getTime()/1000
    var end = task.endDay + ' ' + task.signTime
    var endtTime = (new Date(end)).getTime() / 1000
    wx.showToast({
      title: '新建中',
      icon: 'loading',
      duration: 10000
    })
    // var reqDate = {
    //   number: UserInfo.username,
    //   name: UserInfo.curName,
    //   type: task.name,
    //   lat: task.latitude,
    //   lng: task.longitude,
    //   starttime: startTime,
    //   endtime: endtTime,
    //   // signTime: task.signTime,
    //   // signEarlyTime:task.signEarlyTime,    
    //   // repeat: {
    //   //   'monday': task.repeat.monday,
    //   //   'tuesday': task.repeat.tuesday,
    //   //   'wednesday': task.repeat.wednesday,
    //   //   'thursday': task.repeat.thursday,
    //   //   'friday': task.repeat.friday,
    //   //   'saturday': task.repeat.saturday,
    //   //   'sunday': task.repeat.sunday
    //   // },
    //   // userInfo: {
    //   //   openId: openId,
    //   //   nickName: userInfo.nickName,
    //   //   avatarUrl: userInfo.avatarUrl
    //   // }
    // }
    // console.log(reqDate)
    wx.request({
      url: 'https://nottoday.club/index.php/api/Attendance/createNewAct',
      data: {
        number: UserInfo.username,
        name: UserInfo.curName,
        type: task.name,
        lat: task.latitude,
        lng: task.longitude,
        starttime: startTime,
        endtime: endtTime,
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'Content-Type': 'application/json',
        'cookie': wx.getStorageSync("sessionid")
      }, // 设置请求的 header
      success: function (res) {
        wx.showToast({
          title: '发布成功',
          icon: 'success'
        })
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },

  // 提交、检验
  bindSubmit: function (e) {
    var that = this;
    var task = this.data.task;
    var creating = this.data.creating;
    if (task.name == '' || task.address == '点击选择地点') {
      this.setData({
        modalHidden: false
      });
    } else {
      if (!creating) {
        this.setData({
          'creating': true
        });
        that.createTask();
      }
    }
  },

  // onShow: function () {
  //   // 恢复新建按钮状态
  //   this.setData({
  //     'creating': false
  //   });
  // },

  onHide: function () {
  },

  // 初始化设置
  onLoad: function () {
    var that = this;
    var now = new Date();
    var openId = wx.getStorageSync('openId');

    // 初始化打卡时间
    that.setData({
      'task.signTime': util.formatTime(now).split(" ")[1].split(":")[0] + ":" + util.formatTime(now).split(" ")[1].split(":")[1],
      'task.signEarlyTime': (util.formatTime(now).split(" ")[1].split(":")[0] - 1) + ":" + util.formatTime(now).split(" ")[1].split(":")[1]
    });
    console.log('task.signTime')

    // 初始化日期
    that.setData({
      'task.startDay': util.formatTime(now).split(" ")[0],
      'task.endDay': util.formatTime(now).split(" ")[0]
    });
    console.log(util.formatTime(now))
    // 初始化昵称
    // app.getUserInfo(function (userInfo) {
    //   //更新数据
    //   that.setData({
    //     userInfo: userInfo
    //   });

    //   that.setData({
    //     openId: openId
    //   })
    // });

  }  
})